"""Rename project table to projects, pipeline to pipelines.

Revision ID: fba5d2815f9e
Revises: c276b66607aa
Create Date: 2021-01-27 11:23:32.776281

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "fba5d2815f9e"
down_revision = "c276b66607aa"
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        "projects",
        sa.Column("uuid", sa.String(length=255), nullable=False),
        sa.Column("path", sa.String(length=255), nullable=False),
        sa.Column(
            "status",
            sa.String(length=15),
            server_default=sa.text("'READY'"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("uuid", name=op.f("pk_projects")),
        sa.UniqueConstraint("path", name=op.f("uq_projects_path")),
        sa.UniqueConstraint("uuid", "path", name=op.f("uq_projects_uuid_path")),
    )
    op.create_table(
        "pipelines",
        sa.Column("uuid", sa.String(length=255), nullable=False),
        sa.Column("project_uuid", sa.String(length=255), nullable=False),
        sa.Column("path", sa.String(length=255), nullable=False),
        sa.ForeignKeyConstraint(
            ["project_uuid"],
            ["projects.uuid"],
            name=op.f("fk_pipelines_project_uuid_projects"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("uuid", "project_uuid", name=op.f("pk_pipelines")),
        sa.UniqueConstraint(
            "uuid", "project_uuid", name=op.f("uq_pipelines_uuid_project_uuid")
        ),
    )
    op.drop_table("pipeline")
    op.drop_table("project")
    op.create_unique_constraint(
        op.f("uq_background_tasks_uuid"), "background_tasks", ["uuid"]
    )
    op.create_unique_constraint(op.f("uq_datasources_name"), "datasources", ["name"])
    op.create_unique_constraint(op.f("uq_environments_uuid"), "environments", ["uuid"])


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(op.f("uq_environments_uuid"), "environments", type_="unique")
    op.drop_constraint(op.f("uq_datasources_name"), "datasources", type_="unique")
    op.drop_constraint(
        op.f("uq_background_tasks_uuid"), "background_tasks", type_="unique"
    )
    op.create_table(
        "project",
        sa.Column("uuid", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
        sa.Column("path", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
        sa.Column(
            "status",
            sa.VARCHAR(length=15),
            server_default=sa.text("'READY'::character varying"),
            autoincrement=False,
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("uuid", name="pk_project"),
        sa.UniqueConstraint("path", name="uq_project_path"),
        sa.UniqueConstraint("uuid", "path", name="uq_project_uuid_path"),
        postgresql_ignore_search_path=False,
    )
    op.create_table(
        "pipeline",
        sa.Column("uuid", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
        sa.Column(
            "project_uuid", sa.VARCHAR(length=255), autoincrement=False, nullable=False
        ),
        sa.Column("path", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
        sa.ForeignKeyConstraint(
            ["project_uuid"],
            ["project.uuid"],
            name="fk_pipeline_project_uuid_project",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("uuid", "project_uuid", name="pk_pipeline"),
    )
    op.drop_table("pipelines")
    op.drop_table("projects")
    # ### end Alembic commands ###
